require("babel-preset-es2015");
var parma = process.argv.splice(2);
var http = require("http");
var fs = require("fs");
var path = require("path");
var url = require('url');
var port = parma[0] || 80;
var proxyIp = parma[1];
var httpProxy = require("http-proxy");

/**
 * 获取服务器IP地址
 */
var os = require('os');
let ipAddress = {
    IPv4: {
        address: "localhost"
    }
};

var SYSTEM_NETWORK = os.networkInterfaces();
for (var key in SYSTEM_NETWORK) {
    SYSTEM_NETWORK[key].forEach(function (item) {
        if (!item.internal) {
            ipAddress[item.family] = item;
        }
    })
}
/**
 * 访问请点击
 */
console.log(`hostlink： http://localhost:${port}/`);
console.log(`IPv4link： http://${ipAddress.IPv4.address}:${port}/`);
/**
 * HTTP-PROXY代理
 */
var proxy = httpProxy.createProxyServer({
    target: "http://" + proxyIp
});
/**
 * 代理报错；
 */
proxy.on("error", function (err, req, res) {
    res.writeHead(500, {
        "content-type": "text/plain;charset=utf-8;"
    });
    res.end('接口延迟');
});
/**
 * HTTP服务
 */
http.createServer(function (Request, Response) {
    // 接收 Request；
    // 发送 Response；
    var reqUrl = url.parse(Request.url).pathname;
    if (reqUrl.indexOf('/api') > -1) {
        // 代理接口房问；
        proxy.web(Request, Response);
        return;
    }

    // 解决ASP单页面histany访问问题；
    if (reqUrl == "/" || /\/[0-9a-zA-Z]{1,}$/.test(reqUrl)) {
        reqUrl = "/index.html";
    }

    var pathName = path.join(__dirname, reqUrl);

    fs.readFile(pathName, function (err, stdout) {
        if (!err) {
            var data = stdout;
            // 设置文件类型
            switch (path.extname(pathName)) {
                case ".html":
                    Response.writeHead(200, {
                        "Content-Type": "text/html;charset=utf-8;"
                    });
                    break;
                case ".js":
                    Response.writeHead(200, {
                        "Content-Type": "text/javascript"
                    });
                    break;
                case ".css":
                    Response.writeHead(200, {
                        "Content-Type": "text/css"
                    });
                    break;
                case ".json":
                    Response.writeHead(200, {
                        "Content-Type": "text/json"
                    });
                    break;
                case ".gif":
                    Response.writeHead(200, {
                        "Content-Type": "image/gif"
                    });
                    break;
                case ".jpg":
                    Response.writeHead(200, {
                        "Content-Type": "image/jpeg"
                    });
                    break;
                case ".png":
                    Response.writeHead(200, {
                        "Content-Type": "image/png"
                    });
                    break;
                default:
                    Response.writeHead(200, {
                        "Content-Type": "application/octet-stream"
                    });
            };
            Response.write(data);
            Response.end();
        } else {
            var data = stdout;
            Response.writeHead(200, {
                'Content-type': "text/html;charset=utf-8;"
            });
            Response.write(`<a>${reqUrl}不存在</a>`);
            Response.end();
        }
    });
}).listen(port);